package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.MarketingService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import com.alibaba.fastjson.JSONObject;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeoutException;

/**
 * 转化跟踪
 */
@RestController
@RequestMapping("/api/marketing")
public class MarketingResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(NoteResource.class);
    @Autowired
    private MarketingService marketingService;

    public MarketingResource() throws TimeoutException {
        super("Marketing");
    }

    /**
     * 转化跟踪
     */
    @GetMapping("/getUserActions/{clickId}/{phone}")
    @ApiOperation(value = "转化跟踪回传数据", tags = {"落地广告转化跟踪"}, notes = "转化跟踪回传数据")
    public ResponseEntity<JSONObject> getUserActions(@PathVariable String clickId, @PathVariable String phone) {
        log.info("请求转化跟踪回传数据开始: {}", clickId);
        if (StringUtils.isEmpty(clickId)) {
            throw new ServiceException(MyErrorCode.ERROR_MESSAGE_ISNULL);
        }
        //请求外部系统
        String result = marketingService.getUserActionsByClickID(clickId, phone);
        log.info("请求转化跟踪回传数据结束: {}", result);
        return entityResponse(result);
    }

    /**
     * 授权认证后上传用户行为数据
     */
    @GetMapping("/getAuthorizationCode")
    @ApiOperation(value = "授权认证", tags = {"授权时跳转"}, notes = "授权认证")
    public ResponseEntity<JSONObject> getOauthInfo(@RequestParam String authorization_code) {
        log.info("请求授权认证回传数据开始: {}", authorization_code);
        if (StringUtils.isEmpty(authorization_code)) {
            throw new ServiceException(MyErrorCode.ERROR_MESSAGE_ISNULL);
        }
        // 请求外部系统
        String result = marketingService.getUserActionsByAuthorizationCode(authorization_code);
        log.info("请求授权认证回传数据结束: {}", result);
        return entityResponse(result);
    }

    /**
     * 初次授权认证
     */
    @GetMapping("/getOauth")
    @ApiOperation(value = "授权认证后获取认证code", tags = {"授权时跳转"}, notes = "授权认证获取code")
    public ResponseEntity<JSONObject> getAuthorizationCode(@RequestParam String authorization_code) {
        log.info("授权认证后获取认证code开始: {}", authorization_code);
        if (StringUtils.isEmpty(authorization_code)) {
            throw new ServiceException(MyErrorCode.ERROR_MESSAGE_ISNULL);
        }
        // 请求外部系统
        String result = marketingService.getUserActionsWithAuthorizationCode(authorization_code);
        log.info("授权认证后获取认证code结束: {}", result);
        return entityResponse(result);
    }

    @Scheduled(cron = "0 0 1 * * ?") //每天凌晨1点执行；
    public void refreshTokenTask() {
        log.info("开始执行:refreshTokenTask");
        marketingService.getAccessTokenRefresh();
        log.info("执行结束:refreshTokenTask");
    }
}
